#! /usr/bin/env python

VERSION = '0.1'
APPNAME = 'graphics'

srcdir = '.'
blddir = 'build'

import os, sys

import waf_ddf, waf_dynamo
import waflib.Options
import TestContext
from build_constants import TargetOS

test_context = None

def init(ctx):
    global test_context
    if 'build' in waflib.Options.commands:
        test_context = TestContext.create_test_context()

def options(opt):
    opt.recurse('src')
    opt.load('waf_dynamo')

def configure(conf):
    conf.load('waf_dynamo')
    conf.load('java')
    waf_ddf.configure(conf)

    conf.recurse('src')

    target_os = conf.env['TARGET_OS']
    platform = conf.env['PLATFORM']

    if target_os == TargetOS.LINUX:
        conf.env.append_value('CXXFLAGS', ['-DGL_GLEXT_PROTOTYPES', '-D_GLFW_HAS_GLXGETPROCADDRESS'])
    elif target_os == TargetOS.WEB:
        conf.env.append_value('CXXFLAGS', ['-DGL_GLEXT_PROTOTYPES'])

    if os.getenv('PYTHONPATH'):
        if sys.platform == "win32":
            ddf_search_lst = os.getenv('PYTHONPATH').split(';')
        else:
            ddf_search_lst = os.getenv('PYTHONPATH').split(':')

    dynamo_home = os.getenv('DYNAMO_HOME')
    if not dynamo_home:
        conf.fatal("DYNAMO_HOME not set")
    dynamo_ext = os.path.join(dynamo_home, "ext")

    if target_os == TargetOS.MACOS:
        conf.env.append_value('LINKFLAGS', ['-framework', 'Carbon', '-framework', 'OpenGL', '-framework', 'IOKit', '-framework', 'CoreVideo'])
    elif platform == "x86_64-ios":
        pass
    elif platform == "x86_64-linux":
        conf.env.append_value('LINKFLAGS', ['-lXext', '-lX11', '-lXi', '-lGL', '-lGLU', '-lpthread'])
    elif platform == "arm64-linux":
        conf.env.append_value('LINKFLAGS', ['-lXext', '-lX11', '-lXi', '-lEGL', '-lGLESv1_CM', '-lGLESv2', '-lpthread'])
    elif target_os == TargetOS.WINDOWS:
        conf.env.append_value('LINKFLAGS', ['opengl32.lib'])

    conf.env.append_value('INCLUDES', "../src")
    conf.env.append_value('INCLUDES', os.path.join(dynamo_ext, "include"))
    conf.env.append_value('LIBPATH', os.path.join(dynamo_ext, "lib", platform))
    conf.env.append_value('INCLUDES', os.path.join(dynamo_home, "include" ))
    conf.env.append_value('INCLUDES', os.path.join(dynamo_home, "include", platform))

    conf.env.append_value('DEFINES', 'SDL_JOYSTICK_IOKIT')
    conf.env.append_unique('DEFINES', 'DLIB_LOG_DOMAIN="GRAPHICS"')

def build(bld):
    global test_context
    bld.recurse('src')
    bld.install_files('${PREFIX}/include/win32', bld.path.ant_glob('include/win32/*.h'))
    bld.install_files('${PREFIX}/bin', bld.path.ant_glob('bin/*'), chmod=493)
    TestContext.initialize_test_context(test_context, bld)

def shutdown(self):
    waf_dynamo.run_tests(test_context, valgrind = True)
